package com.jf.blog_jf04.controller;

import com.jf.blog_jf04.bean.Blog;
import com.jf.blog_jf04.data.PageInfo;
import com.jf.blog_jf04.data.dto.BlogDto;
import com.jf.blog_jf04.data.ov.BlogSearchOV;
import com.jf.blog_jf04.data.ov.BlogUpdateOV;
import com.jf.blog_jf04.service.BlogService;
import com.jf.blog_jf04.utils.BeanConvertUtils;
import com.jf.blog_jf04.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/blog")
@RestController
@Api(tags = "博客模块")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @GetMapping("/page")
    @ApiOperation("博客分页筛选")
    public R page(PageInfo pageInfo, BlogSearchOV form) {
        return blogService.getPage(pageInfo, form);
    }

    @PostMapping("/update")
    @ApiOperation("博客添加或修改")
    public R update(@RequestBody BlogUpdateOV form) {
        blogService.saveOrUpdate(form);
        return R.success();
    }

    @GetMapping("/info/{blogId}")
    @ApiOperation("博客详情")
    public R info(@ApiParam("博客编号") @PathVariable int blogId) {
        Blog blog = blogService.getById(blogId);
        return R.success(BeanConvertUtils.copyBean(blog, BlogDto.class));
    }

    @GetMapping("/detail/{blogId}")
    @ApiOperation("博客详情-验证作者")
    public R detail(@ApiParam("博客编号") @PathVariable int blogId) {
        return blogService.getVipBlogDetail(blogId);
    }

    @DeleteMapping("/delete")
    @ApiOperation("博客删除")
    public R delete(@ApiParam("批量删除id") @RequestParam List<Integer> ids) {
        blogService.delete(ids);
        return R.success();
    }

    @PostMapping("/update/status/{blogId}")
    @ApiOperation("修改状态")
    public R updateStatus(@ApiParam("博客编号") @PathVariable int blogId) {
        blogService.updateStatus(blogId);
        return R.success();
    }
}
